<?php
class ModelCatalogCounter extends Model {		
	public function updateVisiter()
	{	
			$this->db->query("UPDATE ".DB_PREFIX."visitor SET counter=counter+1");		
	}
	public function getvisiter()
	{
		$query=$this->db->query("SELECT counter FROM ".DB_PREFIX."visitor");
		$counter=$query->row;
		return $counter['counter'];
	}
	public function getUserOnline()
	{
		$uo_sessionTime = 5; //length in **minutes** to keep the user online before deleting		
		//@mysql_connect($uo_sqlhost, $uo_sqluser, $uo_sqlpass) or die("Users online can't connect to MySQL");
		//@mysql_select_db($uo_sqlbase) or die("Users online can't select database");
		$uo_ip = $_SERVER['REMOTE_ADDR'];
		//cleanup part
		$uo_query = "DELETE FROM ".DB_PREFIX."users_online WHERE unix_timestamp() - lastvisit >= $uo_sessionTime * 60";
		$this->db->query($uo_query);
		//check/insert part
		$uo_query = "SELECT lastvisit FROM ".DB_PREFIX."users_online WHERE visitor = '$uo_ip'";
		$uo_result = $this->db->query($uo_query);
		if(($uo_result->num_rows) == 0) 
		{ //no match
			$uo_query = "INSERT INTO ".DB_PREFIX."users_online VALUES('$uo_ip', unix_timestamp())";
			$this->db->query($uo_query);
		} else 
		{ //matched, update them
			$uo_query = "UPDATE ".DB_PREFIX."users_online SET lastvisit = unix_timestamp() WHERE visitor = '$uo_ip'";
			$this->db->query($uo_query);
		}
		//count part
		
			$uo_query = "SELECT count(*) as total FROM ".DB_PREFIX."users_online";
			$uo_result = $this->db->query($uo_query);
			$uo_count = $uo_result->row;
			return $uo_count['total'];
		
	}
}
?>